package cn.edu.dutir.test.unit.corpus.cwt;

import java.io.File;
import java.io.IOException;
import java.util.Date;

import com.aliasi.corpus.Parser;

import cn.edu.dutir.corpus.cwt.TWCorpus;
import cn.edu.dutir.corpus.cwt.TWURL2DocnoHandler;
import cn.edu.dutir.corpus.cwt.TWURL2DocnoParser;

public class TWURL2Docno {

	private static String output = "E:/Corpus/CWT200g/cwt200g_url_trec";
	private static String input = "E:/Corpus/CWT200g/cwt200g_url_no";
	private static TWCorpus<TWURL2DocnoHandler> mTWCorpus;

	protected static void dohelp() {
		System.out.println("TWURL2Docno <input_dir> <output_dir>");
		System.exit(1);
	}

	public static void main(String args[]) throws IOException {
		if (args.length != 2) {
			dohelp();
		}
		Date start = new Date();
		input = args[0];
		output = args[1];

		TWURL2DocnoParser<TWURL2DocnoHandler> parser = new TWURL2DocnoParser<TWURL2DocnoHandler>();
		TWURL2DocnoHandler handler = new TWURL2DocnoHandler();
		parser.setHandler(handler);

		mTWCorpus = new TWCorpus<TWURL2DocnoHandler>(parser);
		vistCorpus(parser, new File(input));
		handler.close();
		Date end = new Date();
		System.out.println("Time spent: " + (end.getTime() - start.getTime())
				/ 1000);
	}

	public static void vistCorpus(Parser parser, File file) throws IOException {
		if (file.isDirectory()) {
			vistDir(parser, file);
		} else {
			((TWURL2DocnoHandler) parser.getHandler()).setWriter(new File(
					output, file.getName()));
			mTWCorpus.visitCorpus(file);
		}
	}

	public static void vistDir(Parser parser, File dir) throws IOException {
		File files[] = dir.listFiles();
		for (File file : files) {
			vistCorpus(parser, file);
		}
	}
}
